package com.openapi.protocol.decode.subPackageHandle.handle;

import com.alibaba.fastjson.JSONObject;
import com.openapi.config.JT808ServerConfig;
import com.openapi.data.JT808PackageData;
import com.openapi.data.JT808PushData;
import com.openapi.protocol.encode.PackageHj212;
import exsun.jt808.data.config.jt808.Jt808DistributeMsgTopic;
import exsun.jt808.data.distribute_data.jsatl12.AlarmAttachmentUpload;
import exsun.jt808.data.parseUtil.GpsUtil;
import exsun.jt808.data.upload_data.GpsAlarms;
import exsun.jt808.data.upload_data.GpsData;
import exsun.jt808.data.upload_data.Jt808EntityBase;
import exsun.jt808.data.upload_data.jt808.AlarmDetail;
import exsun.jt808.data.util.parse.BitOperator;
import exsun.jt808.data.util.parse.TimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定位信息处理
 */
@Component
public class GpsHandle {
    private static final Logger logger = LoggerFactory.getLogger(GpsHandle.class);

    private static JT808ServerConfig jt808ServerConfig;
    @Autowired
    public void setJt808ServerConfig(JT808ServerConfig jt808ServerConfig) {
        GpsHandle.jt808ServerConfig = jt808ServerConfig;
    }

    public static Jt808EntityBase decode(JT808PackageData packageData)
    {
        byte[] body = packageData.getBodyBuffers();
//        StopWatch stopWatch = new StopWatch();
//        stopWatch.start();
        GpsData data = GpsUtil.parseGps(body,false);

//        String afterDecrypt = JSONObject.toJSONString(data);
//        logger.info(afterDecrypt);
//        stopWatch.stop();
        //logger.info("解析gps消耗时间---" + stopWatch.getTime() / 1000f + "--秒--" + "--设备号--" + data.dvo + "--gps时间--" + data.gdt + "--数据解析时间--" + data.pte);
        return data;
    }

    public static List<JT808PushData> response(GpsData data,String devNo)
    {
        String serviceIp = jt808ServerConfig.getAdasServiceIp();
        int servicePort = jt808ServerConfig.getAdasServicePort();
        List<JT808PushData> orders = new ArrayList<JT808PushData>();

        for (int type:data.adl.keySet()
        ) {
            AlarmDetail detail = data.adl.get(type);
            String alarmCode = getAlarmCode(devNo, type, detail.adt);
            detail.setAmc(alarmCode);
            JT808PushData temp = SendFileUploadOrder(detail.getAfg(), devNo, alarmCode, serviceIp, servicePort);
            orders.add(temp);
            data.adl.put(type, detail);
            logger.info("[{}],{},附件上传指令：{}:{}，报警标志：{}", devNo, GpsAlarms.adasMap.get(type), serviceIp, servicePort, alarmCode);
        }

        List<JT808PushData> list =  new ArrayList<>();
        list.addAll(orders);
        return list;
    }

    private static JT808PushData SendFileUploadOrder(String afg, String devNo,String alarmCode, String serviceIp, int servicePort) {
        AlarmAttachmentUpload obj = new AlarmAttachmentUpload();
        obj.devNo = devNo;
        obj.setIpaddress(serviceIp);
        obj.setUdpPort(0);
        obj.setTcpPort(servicePort);
        obj.setAlarmFlag(afg);
        obj.setAlarmCode(alarmCode);
        obj.setDistributeMsgTopic(Jt808DistributeMsgTopic.AlarmAttachmentUpload_Send);
        return PackageHj212.alarmAttachmentUploadEncode(obj);
    }

    private static String getAlarmCode(String phoneNum, Integer alarmType, Date time){
        //报警编号: 设备号-类型-时间 12+3+12 = 27
        String alarmCode =phoneNum + BitOperator.padLeft(alarmType.toString(),3,'0')
                + TimeUtil.getStringByFormat(time,"yyMMddHHmmss");
        return alarmCode;
    }


}
